var models = require("../models"),
    Good = models.Good,
    Order = models.Order,
    User = models.User,
    WCSession = models.WCSession;
var settions = require('../config');
var wechat_util = require('../utils');
module.exports = function (app) {
    /**
     * 用户续时*/
    app.get("/relet", function (req, res) {
        var sendopenid = req.query.sendopenid;
        var receiveopenid = req.query.receiveopenid;
        var sessionDAO = new WCSession();
        var userDAO = new User();
        //判断用户是否存在status=1的会话
        sessionDAO.findSessionByOpenId({
            status: 1
        }, sendopenid, function (err, sessions) {
            if (sessions.length > 0) {//如果用户存在正在进行的对话
                //判断用户是否还有剩余次数
                userDAO.findUserByOpenid(sendopenid, function (err, user) {
                    if (user) {
                        if (user.count > 0) {
                            //对用户的对话进行延迟操作并扣除用户咨询次数一次
                            //先执行扣除用户一次操作
                            userDAO.updateUserCountByOpneId(sendopenid, function (err) {
                                if (err) {
                                    debug('updateUserCountByOpneId fail', err);
                                } else {
                                    //再执行给用户对话延时
                                    sessionDAO.updateSessionReletByOpenId(sendopenid, receiveopenid, 1, function (err) {
                                        if (err) {
                                            debug('updateSessionReletByOpenId fail', err);
                                            res.render("error", {
                                                message: "出错啦",
                                                error: err
                                            });
                                        } else {
                                            res.render("relet", {
                                                title: "用户咨询续时",
                                                result: "你已经成功续时一次，本次对话已给您延时了" + settions.wc_session_timeout / 60 + "分钟！",
                                                icon: "releted"
                                            });

                                            var sendmsg = "系统：可以继续对话"+ settions.wc_session_timeout / 60 + "分钟！";
                                            var util_tool = new wechat_util();
                                            var msg = {
                                                "sendtextmsg": sendmsg,
                                                "sendopenid": sendopenid
                                            }
                                            util_tool.sendText(msg, function (err) {
                                                if (err) debug("sendText fail", err);
                                            });
                                            sendmsg = "系统："+user.nickname+"续购了"+ settions.wc_session_timeout / 60 + "分钟的对话时长！";
                                            var msg1 = {
                                                "sendtextmsg": sendmsg,
                                                "sendopenid": receiveopenid
                                            }
                                            util_tool.sendText(msg1, function (err) {
                                                if (err) debug("sendText fail", err);
                                            });
                                        }
                                    })
                                }
                            })
                        } else {
                            //余额不足
                            res.render("relet", {
                                title: "用户咨询续时",
                                result: "您目前的咨询次数为0次，点击”用户菜单“下的”购买服务“进行充值。",
                                icon: "reletfail"
                            });
                        }
                    } else {
                        res.render("error", {
                            message: "出错啦",
                            error: err
                        });
                    }
                })
            } else {
                //如果用户不存在正在进行的对话
                res.render("relet", {
                    title: "用户咨询续时",
                    result: "您目前没有正在进行的对话，不能进行此操作。",
                    icon: "reletfail"
                });
            }
        })
    })
}